<template>
  <a-modal
    v-model:visible="visible"
    :title="title"
    :mask-closable="false"
    :esc-to-close="false"
    :width="width >= 600 ? 600 : '100%'"
    draggable
    @before-ok="save"
    @close="reset"
  >
    <GiForm ref="formRef" v-model="form" :columns="columns" />
  </a-modal>
</template>

<script setup lang="ts">
import { Message } from '@arco-design/web-vue'
import { useWindowSize } from '@vueuse/core'
import { addBrandAccount, getBrandAccount, updateBrandAccount } from '@/apis/cakerun/brandAccount'
import { listAllUser } from '@/apis/system/user'
import { type ColumnItem, GiForm } from '@/components/GiForm'
import { useResetReactive } from '@/hooks'

const emit = defineEmits<{
  (e: 'save-success'): void
}>()

const { width } = useWindowSize()

const dataId = ref('')
const visible = ref(false)
const isUpdate = computed(() => !!dataId.value)
const title = computed(() => (isUpdate.value ? '修改美团账号' : '新增美团账号'))
const formRef = ref<InstanceType<typeof GiForm>>()

const [form, resetForm] = useResetReactive({
  // todo 待补充
  enable: true,
})
const userList = ref<any[]>([])

listAllUser({}).then((res) => {
  const data = res.data
  const items: any = []
  data.forEach((item) => {
    items.push({ label: item.nickname, value: item.id.toString() })
  })
  userList.value = items
})
const columns: ColumnItem[] = reactive([
  {
    label: '品牌',
    field: 'brand',
    type: 'input',
    span: 24,
    required: true,
    props: {
      maxLength: 64,
    },
  },
  {
    label: '地区',
    field: 'area',
    type: 'input',
    span: 24,
    required: true,
    props: {
      maxLength: 30,
    },
  },
  {
    label: '账号',
    field: 'account',
    type: 'input',
    span: 24,
    required: true,
    props: {
      maxLength: 30,
    },
  },
  {
    label: '密码',
    field: 'password',
    type: 'input',
    span: 24,
    required: true,
    props: {
      maxLength: 30,
      type: 'password',
    },
  },
  {
    label: '所属人员',
    field: 'userId',
    type: 'select',
    span: 24,
    required: true,
    props: {
      options: userList,
      placeholder: '请选择所属人员',
    },
  },
  {
    label: '门店同步时间',
    field: 'orderShopTime',
    type: 'date-picker',
    span: 24,
  },
  {
    label: '项目同步时间',
    field: 'orderProjectTime',
    type: 'date-picker',
    span: 24,
  },
  {
    label: '订单同步时间',
    field: 'orderDetailTime',
    type: 'date-picker',
    span: 24,
  },
  {
    label: '评价同步时间',
    field: 'feedbackInfoTime',
    type: 'date-picker',
    span: 24,
  },
  {
    label: '指标同步时间',
    field: 'storeMetricsTime',
    type: 'date-picker',
    span: 24,
  },
  {
    label: '状态',
    field: 'enable',
    type: 'switch',
    span: 24,
    required: true,
    prop: {
      type: 'round',
      checkedValue: true,
      uncheckedValue: false,
      checkedText: '启用',
      uncheckedText: '禁用',
    },
  },
])

// 重置
const reset = () => {
  formRef.value?.formRef?.resetFields()
  resetForm()
}

// 保存
const save = async () => {
  try {
    const isInvalid = await formRef.value?.formRef?.validate()
    if (isInvalid) return false
    if (isUpdate.value) {
      await updateBrandAccount(form, dataId.value)
      Message.success('修改成功')
    } else {
      await addBrandAccount(form)
      Message.success('新增成功')
    }
    emit('save-success')
    return true
    // eslint-disable-next-line unused-imports/no-unused-vars
  } catch (error) {
    return false
  }
}

// 新增
const onAdd = async () => {
  reset()
  dataId.value = ''
  visible.value = true
}

// 修改
const onUpdate = async (id: string) => {
  reset()
  dataId.value = id
  const { data } = await getBrandAccount(id)
  Object.assign(form, data)
  visible.value = true
}

defineExpose({ onAdd, onUpdate })
</script>

<style scoped lang="scss"></style>
